package sort;

import java.lang.reflect.Array;
import java.util.Arrays;

public class InsertSort {
    public static void main(String[] args) {
        int arr[] = {43, 98, 12, 1, 5};
        sort(arr);
        System.out.println(Arrays.toString(arr));
    }

    private static void sort(int[] arr) {
        int size = arr.length;
        for(int index = 1; index < size; index++) {
            // index 从后往前移动，找位置
            int val = arr[index];
            int preIndex = index - 1;
            while (preIndex >= 0 && arr[preIndex] > val) {
                arr[preIndex + 1] = arr[preIndex];
                preIndex--;
            }
            arr[preIndex + 1] = val;
        }
    }
}
